export default {
    data(){
        return {
            localButtons:[],
        }
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"detailButtonLang.taskButton",key);
        },
        initButtons(){
            let self = this ;

            this.localButtons.length=0;

            let bpmTask=self.bpmTaskDetail.bpmTask;
            let defaultButton = [
                {
                    id:'btnRestore',
                    method:'showPendingMode',
                    style:false,
                    name:this.i18n('showPendingMode','恢复'),
                    show:bpmTask && bpmTask.status=='PENDING'
                },{
                    id:'approvalHistory',
                    method:'approvalHistory',
                    style:false,
                    name:this.i18n('approvalHistory','审批历史'),
                }, {
                    id:'btnMessage',
                    method:'doMessage',
                    style:false,
                    name:this.i18n('doMessage','留言')
                },
            ]
            /**
             * 正常审批的任务。
             */
            let defaultButtonCanCheck = [
                {
                    id:'btnApprove',
                    method:'approve',
                    style:true,
                    name:this.i18n('approve','审批'),
                    show:bpmTask && bpmTask.status!='PENDING',
                    disabled:!self.allowApprove,
                },
                {
                    id:'btnSave',
                    method:'doSaveData',
                    style:false,
                    name:this.i18n('doSaveData','保存'),
                    loading:self.saveStatus,
                    show:bpmTask && bpmTask.status!='PENDING',
                },
                {
                    id:'btnLock',
                    method:'doLock',
                    style:false,
                    name:self.lockStatus,
                    show:self.candicate!=-1 && !self.isTransferRoam,
                },
                {
                    id:'btnTracked',
                    method:'doTracked',
                    style:false,
                    name:self.trackText
                },
                {
                    id:'btnAddSign',
                    method:'addSign',
                    style:false,
                    name:this.i18n('addSign','加签'),
                    show:self.addSignBtn && !self.isTransferRoam,
                },
                {
                    id:'btnReject',
                    method:'reject',
                    style:false,
                    name:this.i18n('reject','驳回'),
                    show:self.isCanBack && !self.isTransferRoam ,
                },
                {
                    id:'btnRoamTransfer',
                    method:'doRoamTransfer',
                    style:false,
                    name:this.i18n('doRoamTransfer','流转'),
                    show:!self.isCanRoamTransfer && ( bpmTask && bpmTask.status!='PENDING'),
                },
                {
                    id:'btnCancelRoamTransfer',
                    method:'doCancelRoamTransfer',
                    style:false,
                    name:this.i18n('doCancelRoamTransfer','取消流转'),
                    show:self.isCanRoamTransfer,
                },
                {
                    id:'btnRoamInfo',
                    method:'roamInfo',
                    style:false,
                    name:this.i18n('roamInfo','流转情况'),
                    show:self.isCanRoamTransfer || self.isRoamTransferLog,
                },
                {
                    id:'btnTransfer',
                    method:'doTransfer',
                    style:false,
                    name:this.i18n('doTransfer','转办'),
                    show:self.canTransfer && !self.isTransferRoam,
                },
                {
                    id:'btnPending',
                    method:'showPendingMode',
                    style:false,
                    name:this.i18n('btnPending','挂起'),
                    show: bpmTask && bpmTask.status!='PENDING',
                },
                {
                    id:'btnDiscard',
                    method:'doDiscard',
                    style:false,
                    name:this.i18n('doDiscard','作废'),
                    show:self.showDisCardBtn && !self.isTransferRoam,
                },
                {
                    id:'btnCommu',
                    method:'communicate',
                    style:false,
                    name:this.i18n('communicate','沟通'),
                    show:!self.isTransferRoam && ( bpmTask && bpmTask.status!='PENDING'),
                },
                {
                    id:'btnCommuInfo',
                    method:'doCommuHistory',
                    style:false,
                    name:this.i18n('doCommuHistory','沟通情况'),
                    show:self.bpmTaskDetail.communicated && !self.isTransferRoam,
                },{
                    id:'flowChart',
                    method:'flowChart',
                    loading:"flowChartStatus",
                    style:false,
                    name: this.i18n('flowChart','流程图')
                }
            ]
            /**
             * 沟通相关任务
             */
            let communicateTask = [
                {
                    id:'btnCommu',
                    method:'communicate',
                    style:false,
                    name:this.i18n('communicate','沟通'),
                    disabled:!self.bpmTaskDetail.canReply,
                    show:!self.isTransferRoam,
                },
                {
                    id:'btnCommuInfo',
                    method:'doCommuHistory',
                    style:false,
                    name:this.i18n('doCommuHistory','沟通情况')
                },
            ]
            let buttonData =  [];
            if(self.canCheck){
                buttonData = [...defaultButtonCanCheck,...defaultButton]
            }else if(bpmTask && bpmTask.taskType=='LINKUP_TASK' && !self.isTransferRoam){
                buttonData = [...communicateTask,...defaultButton]
            }
            else{
                buttonData=defaultButton;
            }
            let history = [
                {
                    id:'flowChart',
                    method:'flowChart',
                    loading:"flowChartStatus",
                    style:false,
                    name: this.i18n('flowChart','流程图')
                },{
                    id:'approvalHistory',
                    method:'approvalHistory',
                    style:false,
                    name:this.i18n('approvalHistory','审批历史'),
                }
            ]

            if(self.btns.length > 0){//如果有配置按钮 合并配置；
                //自定义按钮
                buttonData = self.btns.map(btn=>{
                    let hasObj = buttonData.find(btnObj=>btn.id === btnObj.id);
                    return hasObj ? {...btn,...hasObj} : {...btn} ;
                })
                buttonData.push(...history)
            }
            let data = buttonData.filter(btn=>{
                if(!btn.hasOwnProperty('show') && btn.method!='doPrint'){
                    return true;
                }
                return  btn.show
            })
            let hasStyleIndex = data.findIndex(item=>{
                return item.style=="true" || item.style==true;
            });
            //更新跟踪按钮名称
            this.initTracked(data);

            this.localButtons.push(...data)  ;
        },
        initTracked(btns){
            let btn = btns.find(btn=>{ return btn.id=="btnTracked" });
            if(btn){
                btn.name = this.trackText;
            }
        },
    },
    watch:{
        btns: {
            handler: function (val, oldval) {
                this.initButtons();
            },
            deep:true,

        }
    }
}
